package com.lagou.homework122;

import java.util.Scanner;

/**
 * 第一阶段模块二第二题作业：
 * 编程实现控制台版并支持两人对战的五子棋游戏
 */
public class Gobang {

    private final static int LENGTH = 17;
    private final static int FLAG_BLACK = -1;
    private final static int FLAG_WHITE = -100;

    private int count = 0;

    Player player = Player.Black;
    int[][] boardArray = new int[LENGTH][LENGTH]; //棋盘数组

    public Gobang() {
        //初始化数组
        for (int i = 0; i < LENGTH - 1; i++) {
            boardArray[0][i + 1] = i;
            boardArray[i + 1][0] = i;
        }
    }

    //（1）绘制棋盘 - 写一个成员方法实现
    public void drawBoard() {
//        for (int i = 0; i < LENGTH; i++)
//            System.out.println(Arrays.toString(arrays[i]));
        System.out.println("-------------------------------------------------------------");
        //逐行绘制五子棋游戏棋盘
        for (int i = 0; i < LENGTH; i++) {
            // 第一行
            if (i == 0)
                for (int j = 0; j < LENGTH; j++) {
                    if (j > 0)
                        System.out.print(Integer.toHexString(j - 1) + "\t");
                        // System.out.print((char)(j+87)+" ");
                    else
                        System.out.print(" \t");
                }
            else
                for (int j = 0; j < LENGTH; j++) {
                    if (j > 0) {
                        int num = boardArray[i][j];
                        if (num == FLAG_BLACK)
                            System.out.print("&\t");
                        else if (num == FLAG_WHITE)
                            System.out.print("#\t");
                        else
                            System.out.print("+\t");
                    } else
                        System.out.print(Integer.toHexString(i - 1) + "\t");
                    // System.out.print((char)(i+87)+" ");
                }
            System.out.println();
        }
    }

    public boolean checkInput(int x, int y) {
        if (x > 15 || x < 0) {
            System.out.println("横坐标不合法！");
            return false;
        }
        if (y > 15 || y < 0) {
            System.out.println("纵坐标不合法！");
            return false;
        }
        return true;
    }

    //落子，设置坐标值
    public boolean setChessMan(int x, int y, Player player) {
//        if (x == 0 || y == 0) {
//            System.out.println("坐标点无效！");
//            return false;
//        }
        if (boardArray[x][y] < 0) {
            System.out.println("此坐标点已落子，请重新输入坐标点！");
            return false;
        }
        //黑棋 -1 ，白棋 -2
        boardArray[x][y] = (player == Player.Black) ? FLAG_BLACK : FLAG_WHITE;
//        System.out.println("--落子有效---");
        count++;
        return true;
    }

    //（2）提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
    public void play() {
//        count++;
        //偶数,白棋下完
        if (count % 2 == 0) {
            player = Player.Black;
        } else {
            player = Player.White;
        }
//        System.out.println("请<" + (player == Player.Black ? "黑棋" : "白棋") + ">选手输入落子坐标，横纵坐标有效区间0~15 : ");
        drawBoard();
    }

    public static void main(String[] args) {

        Gobang gobang = new Gobang();
        gobang.drawBoard();

        boolean playing = true;

        System.out.println("-------------------------------------------------------------");
        System.out.println("游戏规则：黑棋先手");

        while (playing) {

            System.out.println("请<" + (gobang.player == Player.Black ? "黑棋&" : "白棋#") + ">选手输入落子坐标，横纵坐标有效区间0~15 : ");
            Scanner scanner = new Scanner(System.in);
            int x = scanner.nextInt();
            int y = scanner.nextInt();
//            scanner.close(); //NoSuchElementException
            if (gobang.checkInput(x, y)) {
                //x+1,y+1,坐标点偏移
                boolean res = gobang.setChessMan(x + 1, y + 1, gobang.player);
                //落子有效
                if (res) {
//                    System.out.println("gobang.count = " + gobang.count);
                    //（3）每当一方下棋后判断是否获胜
                    if (Referee.check(gobang.boardArray, x + 1, y + 1)) {
                        gobang.drawBoard();
                        System.out.println((gobang.count % 2 == 1 ? "<黑棋&" : "<白棋#") + "> 胜利！\n游戏结束！");
                        playing = false;
                    }
//                        break;
                    else {
                        gobang.play();
                    }

                }
            }
        }

    }
}
